<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
<!--			<el-form-item label="上级角色" prop="parentId">
				<el-cascader v-model="form.parentId" :options="groups" :props="groupsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
			</el-form-item>-->
			<el-form-item label="角色名称" prop="name">
				<el-input v-model="form.name" clearable></el-input>
			</el-form-item>
			<el-form-item label="唯一标识" prop="label">
				<el-input v-model="form.label" clearable></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" clearable></el-input>
			</el-form-item>
			<el-form-item label="菜单权限" prop="remark">
				<el-tabs tab-position="top" style="width: 100%;">
					<el-tab-pane label="功能权限">
						<div class="treeMain">
							<el-tree ref="menu" node-key="id" :data="menu.list" :default-checked-keys="menu.checked"
									 :props="menu.props" show-checkbox check-strictly default-expand-all>
								<template #default="{node, data}">
							<span class="custom-tree-node el-tree-node__label">
								<span class="label">
									{{ node.label }}
									<el-icon color="rgb(121, 187, 255)" style="margin-left: 5px;" v-if="data.type === 0"><el-icon-wallet /></el-icon>
									<el-icon color="rgb(238, 190, 119)" style="margin-left: 5px;" v-if="data.type === 1"><el-icon-tickets /></el-icon>
									<el-icon color="rgb(200, 201, 204)" style="margin-left: 5px;" v-if="data.type === 2"><el-icon-tools /></el-icon>
									<template v-if="data.is_show === 0"><el-tag style="margin-left: 5px;" type="warning">隐藏</el-tag></template>
								</span>
							</span>
								</template>
							</el-tree>
						</div>
					</el-tab-pane>
					<el-tab-pane label="数据权限">
						<div class="treeMain">
							<el-tree ref="group" node-key="id" :data="group.list" :default-checked-keys="group.checked"
									 :props="group.props" show-checkbox></el-tree>
						</div>
					</el-tab-pane>
				</el-tabs>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import api from '@/api/index.js'
import tool from '@/utils/tool.js'
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:0,
					label: "",
					name: "",
					remark: "",
					sort: 1,
					is_org: 0,
					parentId: ""
				},
				//验证规则
				rules: {
					sort: [
						{required: true, message: '请输入排序', trigger: 'change'}
					],
					name: [
						{required: true, message: '请输入角色名称'}
					],
					label: [
						{required: true, message: '请输入唯一标识'}
					]
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					emitPath: false,
					checkStrictly: true
				},
				menu: {
					list: [],
					checked: [],
					props: {
						label: (data)=>{
							return data.name
						}
					}
				},
				group: {
					list: [],
					checked: [],
					props: {
						label: (data)=>{
							return data.name
						}
					}
				},
			}
		},
		mounted() {
			this.getMenu()
			this.getGroup()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			async getMenu(){
				var res = await api.system.menu.list()
				const routes = res.data.map(e => {
					return {
						id: e.id,
						parent_id: e.parent_id,
						router: tool.revisePath(e.router || String(e.id)),
						view_path: e.view_path,
						type: e.type,
						name: e.name,
						icon: e.icon,
						sort: e.sort,
						perms: e.perms.split(',').map(p => { return {text:p} }),
						is_show: tool.isEmpty(e.is_show) ? true : e.is_show,
						children: []
					};
				})
				this.menu.list = tool.deepTree(routes,'parent_id');
			},
			//加载树数据
			async getGroup(){
				var res = await api.system.dept.list()
				this.group.list = tool.deepTree(res.data,'parent_id');
				// this.groups = res.data;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						let formData = {}
						Object.assign(formData, this.form)
						formData.menuIdList = [...this.$refs.menu.getCheckedKeys(),...this.$refs.menu.getHalfCheckedKeys()];
						formData.departmentIdList = [...this.$refs.group.getCheckedKeys(),...this.$refs.group.getHalfCheckedKeys()];
						let methodName;
						if (formData.id > 0) {
							methodName = 'update'
						}
						else {
							methodName = 'add'
						}
						let res = await api.system.role[methodName](formData);
						this.isSaveing = false;
						if(res.code === 0){
							this.$emit('success', formData, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			async getInfoData(id) {
				this.isSaveing = true;
				let res = await api.system.role.info({id:id});
				this.isSaveing = false;
				if(res.code === 0){
					this.form = res.data
					this.group.checked = res.data.departmentIdList
					this.menu.checked = res.data.menuIdList
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//表单注入数据
			async setData(data){
				/*this.form.id = data.id
				this.form.label = data.label
				this.form.alias = data.alias
				this.form.sort = data.sort
				this.form.parentId = data.parentId*/

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				// Object.assign(this.form, data)
				this.getInfoData(data.id)
			}
		}
	}
</script>

<style scoped>
.treeMain {height:280px;overflow: auto;border: 1px solid #dcdfe6;margin-bottom: 10px;}
</style>
